<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2020/06/02
 * Time: 14:58
 */

namespace business\elasticsearch\services;

use business\elasticsearch\models\ProcurementModel;

class ProcurementService
{
    public function getCount($categoryCode = '')
    {
        $condition = [];
        // 构建过滤条件
        $this->builderFilter($condition, $categoryCode);
        $total = ProcurementModel::find()->query($condition)->count();
        $result = ['resultNum' => $total];

        return $result;
    }

    /**
     * 构建过滤条件
     * @param array $query
     * @param string $categoryCode
     */
    protected function builderFilter
    (
        &$query = [],
        $categoryCode = ''
    ) {
        $categoryCode = !empty($categoryCode) ? explode(',', $categoryCode) : '';
        $categoryCode = count($categoryCode) > 1 ? $categoryCode : current($categoryCode);
        $query['function_score']['query']['bool']['filter'][] = ProcurementModel::basicCategoryCode($categoryCode, 3);
    }
}
